package com.cyanite.playlv2;

import com.cyanite.playlv2.widget.SecretFollowsAdapter;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Secret;
import com.cyanite.playlv2api.types.User;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class SecretFollowsActivity extends Activity {

    public static final String EXTRA_SECRET_ID = PlayLv2.PACKAGE_NAME
        + ".SecretFollowsActivity.EXTRA_SECRET_ID";

    private View mViewProgressBar;
    private StateHolder mStateHolder;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secret_follows_activity);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(PlayLv2.INTENT_ACTION_LOGGED_OUT));

        mStateHolder = new StateHolder();
        if (getIntent().hasExtra(EXTRA_SECRET_ID)) {
            mStateHolder.setSecretId(getIntent().getExtras().getString(EXTRA_SECRET_ID));
        }   else {
            finish();
            return;
        }
        mViewProgressBar = findViewById(R.id.secretFollowsProgress);
        mViewProgressBar.setVisibility(View.VISIBLE);
        mStateHolder.startTaskSecretFollowsHistory(this);

        ensureUi();
    }

    private void ensureUi() {
        ListView mListView = (ListView)findViewById(R.id.lvSecretFollowsList);
        if ( mStateHolder.getUsers() != null )  {
            SecretFollowsAdapter mAdapter = new SecretFollowsAdapter(this, mStateHolder.getUsers());
            mListView.setAdapter(mAdapter);
        }
    }

    private void onGetSecretFollowsTaskComplete(Secret secret, Exception ex) {
        mViewProgressBar.setVisibility(View.GONE);
        if (secret != null) {
            mStateHolder.setSecret(secret);
            ensureUi();
        }
    }

    private static class GetSecretFollowsTask extends AsyncTask<Void, Void, Secret> {

        private SecretFollowsActivity mActivity;
        private String mSecretId;
        private Exception mReason;

        public GetSecretFollowsTask(SecretFollowsActivity activity, String secretId) {
            mActivity = activity;
            mSecretId = secretId;
        }

        @Override
        protected Secret doInBackground(Void... params) {
            try {
                return ((PlayLv2) mActivity.getApplication()).getPlayLv2Api().secret(mSecretId);
            } catch (Exception ex) {
                mReason = ex;
                return null;
            }
        }

        @Override
        protected void onPostExecute(Secret secret) {
            if (mActivity != null) {
                mActivity.onGetSecretFollowsTaskComplete(secret, mReason);
            }
        }
    }

    private static class StateHolder {

        private String mSecretId;
        private Group<User> mUsers;
        private GetSecretFollowsTask mGetSecretFollowsTask;

        public StateHolder() {
            mUsers = new Group<User>();
        }

        public void setSecretId(String id)  {
            mSecretId = id;
        }

        public Group<User> getUsers() {
            return mUsers;
        }

        public void setSecret(Secret secret)    {
        }

        public void startTaskSecretFollowsHistory(SecretFollowsActivity activity) {
            mGetSecretFollowsTask = new GetSecretFollowsTask(activity, mSecretId);
            mGetSecretFollowsTask.execute();
        }
    }
}
